package com.examination.service.impl;

import com.examination.bean.ExamePaper;
import com.examination.bean.ExamePaperDetail;
import com.examination.bean.ExamePaperScore;
import com.examination.bean.Selection;
import com.examination.dao.ExamePaperDetailDao;
import com.examination.dao.SelectionAdminDao;
import com.examination.service.IExamePaperDetailService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: GongXl
 * @Date: 027 16:37
 * @Description:
 */
public class ExamePaperDetailService implements IExamePaperDetailService {
    private ExamePaperDetailDao examePaperDetailDao = new ExamePaperDetailDao();
    private SelectionAdminDao selectionAdminDao = new SelectionAdminDao();

    public List<Map> getExamePaperDetailList(ExamePaperDetail examePaperDetail){
        List<Map> list = examePaperDetailDao.getExamePaperDetailList(examePaperDetail);
        return list;
    }

    @Override
    public List<ExamePaper> getExamePaperList(ExamePaper examePaper) {
        List<ExamePaper> list = examePaperDetailDao.getExamePaperList(examePaper);
        return list;
    }

    @Override
    public String addExamePaperScore(ExamePaperScore examePaperScore) {
        return examePaperDetailDao.addExamePaperScore(examePaperScore);
    }

    @Override
    public List<Map> getExamePaperQuestMapList(ExamePaperScore examePaperScore,Map<String,String[]> answerMap) {
        List<Map> questMapList = examePaperDetailDao.getExamePaperQuestMapList(examePaperScore);
        String selectionIds = "";
        String multiChoiceIds = "";
        for (int i = 0; i < questMapList.size(); i++) {
            Map questMap = questMapList.get(i);
            Object scheme_type = questMap.get("scheme_type");
            if(null != scheme_type){
                Object quest_id = questMap.get("quest_id");
//                single_choice--单选，multi_choice--多选,judge--判断题，fill_blank--填空题，simple_question--简答题'',
                switch (scheme_type.toString()){
                    case "single_choice":{
                        selectionIds += "," + quest_id;
                        break;
                    }
                    case "multi_choice":{
                        multiChoiceIds += "," + quest_id;
                        break;
                    }default:{

                    }
                }
            }
        }
        //单选的类型的题目
        if(selectionIds.length() > 0){
            //查询所有单选题的集合
            Selection selection = new Selection();
            Map args = new HashMap();
            args.put("selectionIds",selectionIds.substring(1));
            selection.setArgs(args);
            //选择题库的标准信息，有正确答案
            List<Selection> selectionList = selectionAdminDao.getSelectionListByIds(selection);

            for (int i = 0; i < selectionList.size();i++) {
                Selection selectionTemp = selectionList.get(i);
                String questId = selectionTemp.getId()+"";
                //single_choice#2#answer1
                String nameType = "single_choice#"+questId+"#answer"+(i+1);
                String resAnswer = "";//学生答案
                String[] stu_answer = answerMap.get(nameType);
                if(null != stu_answer){
                    resAnswer = stu_answer[0];
                }
                //真实值
                String trueAnswer = selectionTemp.getAnswer();
                if(trueAnswer.equals(resAnswer)){
                    //TODO 答案正确更新分
                    //存入到对应的题目答案详情列表
                    //后期老师要确认是评分完毕。
                    //系统自动评完分就完毕了
                }
            }
            //和同学提交的数据进行比较


        }
        return questMapList;
    }
}